package handler

import (
	"biz-manager/cache"
	"biz-manager/moder"
	"encoding/json"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

func GoodsAdd(c *gin.Context) {
	GoodsDepot := c.PostForm("GoodsDepot")
	GoodsName := c.PostForm("GoodsName")
	if GoodsDepot == "" || GoodsName == "" {
		c.JSON(200, gin.H{
			"mas":  "不能为空",
			"code": 500,
		})
		return
	}

	data := moder.Goods{
		GoodsDepot: GoodsDepot,
		GoodsName:  GoodsName,
	}
	err := moder.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"mas":  "失败",
			"code": 500,
		})
		return
	}
	c.JSON(200, gin.H{
		"mas":  "成功",
		"code": 200,
		"data": data,
	})
	return

}

func OrderAdd(c *gin.Context) {
	OrderNum := c.PostForm("OrderNum")
	OrderState := c.PostForm("OrderState")
	OrderPrice, _ := strconv.Atoi(c.PostForm("OrderPrice"))
	Gid, _ := strconv.Atoi(c.PostForm("Gid"))
	if OrderNum == "" || OrderState == "" || OrderPrice == 0 || Gid == 0 {
		c.JSON(200, gin.H{
			"mas":  "不能为空",
			"code": 500,
		})
		return
	}

	data := moder.Order{
		OrderNum:   OrderNum,
		OrderState: OrderState,
		OrderPrice: OrderPrice,
		Gid:        Gid,
	}

	err := moder.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"mas":  "失败",
			"code": 500,
		})
		return
	}
	c.JSON(200, gin.H{
		"mas":  "成功",
		"code": 200,
		"data": data,
	})
	return

}

func GoodsOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	if size > 50 {
		c.JSON(200, gin.H{
			"mas":  "size≤50",
			"code": 500,
		})
		return
	}
	offset := (page - 1) * size
	var data []moder.GoodsOrder
	redis_key := "redis_list" + strconv.Itoa(page)
	result, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if result == "" {
		moder.DB.Table("orders").
			Select("orders.id AS ID",
				"orders.order_num AS OrderNum",
				"order_state AS OrderState",
				"order_price AS OrderPrice",
				"goods_depot AS GoodsDepot",
				"goods_name AS GoodsName").
			Joins("LEFT JOIN goods ON goods.id = orders.gid").
			Offset(offset).
			Limit(size).
			Order("id asc").
			Debug().
			Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &data)
	}

	var total int64
	moder.DB.Model(&moder.Order{}).Count(&total)
	c.JSON(200, gin.H{
		"mas":   "成功",
		"code":  200,
		"data":  data,
		"total": total,
	})
	return

}

func GoodsOrderDetail(c *gin.Context) {
	id := c.Query("id")

	var data moder.GoodsOrder
	redis_key := "redis_detail" + id
	result, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if result == "" {
		moder.DB.Table("orders").
			Select("orders.id AS ID",
				"orders.order_num AS OrderNum",
				"order_state AS OrderState",
				"order_price AS OrderPrice",
				"goods_depot AS GoodsDepot",
				"goods_name AS GoodsName").
			Joins("LEFT JOIN goods ON goods.id = orders.gid").
			Where("orders.id =?", id).
			Debug().
			Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &data)
	}

	c.JSON(200, gin.H{
		"mas":  "成功",
		"code": 200,
		"data": data,
	})
	return

}
